package com.osgix.common.pojo.temp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>权限操作位</b>
 *
 * <b>@author zqmao 2007-7-31,下午03:36:06</b>
 *
 * <b>version 1.0</b>
 */
public class Operand implements Serializable, Comparable {

	private static final long serialVersionUID = 1L;

	/**
	 * 默认操作ID
	 */
	public static final String DEFAULT_ID = "P001";

	/**
	 * 默认Privilege的KEY
	 */
	public static final String DEFAULT_KEY = "p0";

	/**
	 * 资源访问类型
	 */
	private String accessId = "";

	/**
	 * 操作ID
	 */
	private String id = "";

	/**
	 * 操作位
	 */
	private int bit = -1;

	/**
	 * 操作显示提示
	 */
	private String desc = "";

	/**
	 * 依赖
	 */
	private List relies = new ArrayList();

	/**
	 * 权限值对应的键值(多个权限值时)
	 */
	private String key = DEFAULT_KEY;
	/**
	 * 选择状态
	 */
	private String checked;

	/**
	 * 构造函数
	 *
	 */
	public Operand() {

	}

	/**
	 * 操作位
	 * @param id
	 * @param bit
	 * @param desc
	 */
	public Operand(String accessId, String id, int bit, String desc) {
		this.accessId = accessId;
		this.id = id;
		this.bit = bit;
		this.desc = desc;
	}

	/**
	 * 操作位
	 * @param id
	 * @param bit
	 * @param desc
	 */
	public Operand(String accessId, String id, int bit, String desc, String key) {
		this.accessId = accessId;
		this.id = id;
		this.bit = bit;
		this.desc = desc;
		this.key = key;
	}

	/**
	 * 操作位
	 * @param id
	 * @param bit
	 * @param desc
	 */
	public Operand(String accessId, String id, int bit, String desc, String key, List rely) {
		this.accessId = accessId;
		this.id = id;
		this.bit = bit;
		this.desc = desc;
		this.key = key;
		for (int i = 0; i < rely.size(); i++)
			addRely((String) rely.get(i));
	}

	/**
	 * 克隆方法
	 */
	public Object clone() {
		Operand oper = new Operand(accessId, id, bit, desc, key, relies);
		return oper;
	}

	/**
	 * 添加一个依赖
	 *
	 */
	public void addRely(String operandId) {
		if (!relies.contains(operandId))
			relies.add(operandId);
	}

	/**
	 * 删除一个依赖
	 * @param operandId
	 */
	public void removeRely(String operandId) {
		if (relies.contains(operandId))
			relies.remove(operandId);
	}

	public int getBit() {
		return bit;
	}

	public void setBit(int bit) {
		this.bit = bit;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAccessId() {
		return accessId;
	}

	public void setAccessId(String accessId) {
		this.accessId = accessId;
	}

	public long getBitValue() {
		return new Double(Math.pow(2, bit)).longValue();
	}

	public List getRelies() {
		return relies;
	}

	public void setRelies(List relies) {
		this.relies = relies;
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Object arg0) {
		Operand objOper = (Operand) arg0;
		return (bit - objOper.getBit());
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String toString() {
		return accessId + "/" + id + "/" + desc + "/" + bit + "/" + key + "/" + relies;
	}

}
